package com.qf.fc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.fc.Param.MoodAdd;
import com.qf.fc.dao.MoodDao;
import com.qf.fc.dto.MoodDetail;
import com.qf.fc.entity.Mood;
import com.qf.fc.entity.Topic;
import com.qf.fc.service.MoodService;
import com.qf.fc.utils.AliOSSUtils;
import com.qf.fc.vo.R;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 动态表(Mood)表服务实现类
 *
 * @author makejava
 * @since 2024-09-23 23:34:47
 */
@Service("moodService")
@RequiredArgsConstructor
public class MoodServiceImpl implements MoodService {
    private final MoodDao dao;
    @Autowired
    private AliOSSUtils aliOSSUtils;

    @Override
    public R add(MoodAdd add) {
        //1.验证
        if (add==null && StringUtils.hasLength(add.getContent())){
            return R.fail("亲，请检查参数信息");
        }
        //组装对象
        Mood mood = new Mood();
        BeanUtils.copyProperties(add,mood);
        mood.setStatus("未审核");
        LocalDateTime now = LocalDateTime.now();
        if (now.getHour()>=6 && now.getHour()<=12){
            mood.setType("上午");
        }else if (now.getHour()>12 && now.getHour()<=18){
            mood.setType("下午");
        }else {
            mood.setType("晚上");
        }
        mood.setCreateTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        if (dao.insert(mood)>0){
            return R.ok("ok");
        }
        return R.fail("亲，网络异常，请检查");
    }

    @Override
    public R queryMy(Integer uid) {
        return R.ok(dao.selectByUid(uid));
    }

    @Override
    public R queryPage(String type,int page,int size) {
        //最新 ，热门
        String order = "m.create_time";
        if (StringUtils.hasLength(type) && type.equals("热门")){
            order="m.read_num";
        }
        //分页
        //每一页显示十条数据，查询第三页，起始行索引从？开始
        //第一页：0，10
        //第二页：10，10
        List<MoodDetail> data = dao.selectLimit(order,(page-1)*size,size);
        Long total = dao.selectCount();
        Map<String,Object> result = new HashMap<>();
        result.put("total",total);
        result.put("pages",total%size==0?total/size:total/size+1);
        result.put("data",data);
        return R.ok(result);
    }

    @Override
    public R queryDetail(Integer id) {
        //阅读量+1
        dao.updateReads(id);
        //查询 详情数据信息
        return R.ok(dao.selectMid(id));
    }

    @Override
    public R queryById(String uid) {
        return R.ok(dao.selectByMyUid(uid));
    }
}

